import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import { FinancialCostServiceProxy, FinancialCostOfDeviceListDto } from '@shared/service-proxies/service-proxies';
//import { ExcelModalComponent } from '@app/shared/common/excel-modal/excel-modal.component';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDatepickerConfig, BsDaterangepickerConfig } from 'ngx-bootstrap';
import { CreateOrEditFinancialCostModalComponent } from './create-or-edit-financial-cost-modal.component';
//import { CreateOrEditSparePartModalComponent } from './create-or-edit-spare-part-modal.component';

@Component({
    templateUrl: './financial-cost.component.html',
    animations: [appModuleAnimation()]
})
export class FinancialCostComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    // @ViewChild('excelModal') excelModal: ExcelModalComponent;
    @ViewChild('createOrEditFinancialCostModal')
    createOrEditFinancialCostModal: CreateOrEditFinancialCostModalComponent;

    anyncing = false;
    filterText = '';
    deviceId?: number;
    year?: number;
    workshop = '';
    deviceNumber = '';
    deviceName = '';
    deviceType?: number;
    public dateRange: Date[] = [
        moment()
            .startOf('day')
            .toDate(),
        moment()
            .endOf('day')
            .toDate()
    ];
    // userDataPermissions;
    // selectedDepts;
    selectedValues = new Array<FinancialCostOfDeviceListDto>();
    // minMode: BsDatepickerViewMode = 'month';

    // bsConfig: Partial<BsDaterangepickerConfig>;
    // extensions: Array<ListDtoExtensionsItem>;
    constructor(injector: Injector, private _financialCostServiceProxy: FinancialCostServiceProxy) {
        super(injector);
    }

    ngOnInit() {}

    getTableData(event?: LazyLoadEvent) {
        this.filterText = '';
        this.deviceNumber = '';
        this.deviceName = '';
        let years: number;
        this.year = years;
        this.workshop = '';
        this.primengTableHelper.showLoadingIndicator();

        this._financialCostServiceProxy
            .getAllAsync(
                this.year,
                this.workshop,
                this.deviceNumber,
                this.deviceName,
                this.deviceType,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    getAllAsync(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();

        this._financialCostServiceProxy
            .getAllAsync(
                this.year,
                this.workshop,
                this.deviceNumber,
                this.deviceName,
                this.deviceType,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    createOrEditFinancialCost(): void {
        this.createOrEditFinancialCostModal.show();
    }
    delete(FinancialCostOfDevice: FinancialCostOfDeviceListDto) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            let self = this;

            if (isConfirmed) {
                this._financialCostServiceProxy.deleteAsync(FinancialCostOfDevice.id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    self.getAllAsync();
                });
            }
        });
    }
}
